package com.chaodev.base.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.chaodev.base.dao.ExplanationLineDao;
import com.chaodev.base.dto.ExplanationLineDto;
import com.chaodev.base.model.ExplanationLine;
import com.chaodev.base.model.ExplanationPoint;
import com.chaodev.base.service.ExplanationLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ExplanationLineServiceImpl implements ExplanationLineService {

    @Autowired
    private ExplanationLineDao explanationLineDao;

    @Override
    public boolean addExplanationLine(ExplanationLine explanationLine) {
        return explanationLineDao.insertExplanationLine(explanationLine) != 0;
    }

    @Override
    public boolean editExplanationLine(ExplanationLine explanationLine) {
        return explanationLineDao.updateExplanationLine(explanationLine) != 0;
    }

    @Override
    public boolean saveExplanationLine(ExplanationLine explanationLine) {
        if (ObjectUtil.isNotNull(explanationLine.getId()) && !"".equals(explanationLine.getId())){
            explanationLine.setUpdateUser(explanationLine.getCreateUser());
            return editExplanationLine(explanationLine);
        }
        return addExplanationLine(explanationLine);
    }

    @Override
    public boolean removeExplanationLine(ExplanationLine explanationLine) {
        return explanationLineDao.deleteExplanationLine(explanationLine.getId()) != 0;
    }

    @Override
    public List<ExplanationLine> getExplanationLineList(ExplanationLineDto explanationLineDto) {
        return explanationLineDao.selectExplanationLineList(explanationLineDto);
    }

    @Override
    public ExplanationLine getExplanationLineById(ExplanationLine explanationLine) {
        ExplanationLine result = explanationLineDao.selectExplanationLineById(explanationLine.getId());
        String[] split = result.getPointId() == null || "".equals(result.getPointId()) ? new String[0] : result.getPointId().split(",");
        if (split.length > 0){
            List<ExplanationPoint> explanationPointList = explanationLineDao.selectExplanationPointByLine(result.getPointId());
            List<ExplanationPoint> explanationPointSort = new ArrayList<>();
            for (String s : split){
                for (ExplanationPoint e : explanationPointList){
                    if(s.equals(e.getId())){
                        explanationPointSort.add(e);
                    }
                }
            }
            result.setPointList(explanationPointSort);
        }
        return result;
    }

    @Override
    public PageInfo<ExplanationLine> getExplanationLinePage(ExplanationLineDto explanationLineDto) {
        PageHelper.startPage(explanationLineDto.getPage(),explanationLineDto.getRows());
        List<ExplanationLine> list = explanationLineDao.selectExplanationLineList(explanationLineDto);
        PageInfo<ExplanationLine> pageInfo = new PageInfo<>(list, explanationLineDto.getRows());
        return pageInfo;
    }
}
